package org.mcjug.locationmailer.util;

import org.mcjug.locationmailer.R;
import org.mcjug.locationmailer.receiver.ExceptionBroadcastReceiver;

import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;
//import android.util.Log;

public class ExceptionUtil {

    public static void exceptAway(Context context, Throwable throwable, String extraInfo,
            RunnableWithException cleanupTasks) {
        if (cleanupTasks != null) {
            try {
                cleanupTasks.run();
            } catch (Throwable e) {
                // An exception occurred while we were trying to clean up after another exception occuring, not much we
                // can
                // do.
                //Log.e(ExceptionUtil.class.getSimpleName(), "error trying to cleanup after previous error", e);
            }
        }

        Throwable extraInfoThrowable = new Throwable(extraInfo + throwable.getMessage(), throwable);
        ParcelableException parcelableException = new ParcelableException(extraInfoThrowable);
        Intent exceptionIntent = new Intent(context.getString(R.string.intentExceptionActionName));
        exceptionIntent.putExtra(ExceptionBroadcastReceiver.EXCEPTION, (Parcelable) parcelableException);
        context.sendBroadcast(exceptionIntent);
    }

}
